package com.ss.android.common.applog;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;

import java.util.Date;

public final class ab implements Callback {
    public interface ab$a {
        void onTrafficWarning(TrafficWarningInfo arg1);
    }

    final class TrafficWarningInfo {
        public boolean tx;
        public long cost;
        public long last;
        public long current;
        public long lastTime;
        public long initTime;
        public boolean isAccumulate;

        TrafficWarningInfo() {

        }

        public final String toString() {
            return "TrafficWarningInfo (tx:: " + this.tx
                    + ", cost: " + this.cost
                    + ", last: " + this.last
                    + ", current:" + this.current
                    + ", lastTime:" + this.lastTime
                    + ", initTime:" + this.initTime
                    + ", isAccumulate:" + this.isAccumulate + ")";
        }
    }

    final Handler a;
    private final Context b;
    private final ab$a c;
    private long[] d;
    private long[] e;
    private long[] f;
    private long g;
    private long h;
    private int i;
    private long j;

    public ab(Context context, ab$a arg4) {
        this.d = new long[2];
        this.e = new long[2];
        this.f = new long[2];
        this.i = 0;
        this.j = 0;
        this.b = context.getApplicationContext();
        this.a = new WeakHandler(Looper.getMainLooper(), this);
        this.c = arg4;
    }

    public final void a() {
        this.a.sendEmptyMessage(3);
    }

    private void a(long[] arg5) {
        if (arg5 != null && arg5.length >= 2) {
            long v2 = -1;
            try {
                arg5[0] = v2;
                arg5[1] = -1;
                ApplicationInfo v0_1 = this.b.getApplicationInfo();
                if (v0_1 == null) {
                    return;
                }

                if (v0_1.uid <= 0) {
                    return;
                }
                arg5[0] = TrafficStats.getUidTxBytes(v0_1.uid);
                arg5[1] = TrafficStats.getUidRxBytes(v0_1.uid);
            } catch (Throwable v0) {
            }
        }
    }

    public final void handleMsg(Message message) {
        boolean v0_1;
        TrafficWarningInfo v3;
        long v6;
        switch (message.what) {
            case 1: {
                try {
                    long[] v2 = this.d;
                    this.a(v2);
                    if (Logger.debug()) {
                        Logger.d("TrafficGuard", "check traffic: " + this.i + " " + v2[0] + " " + v2[1] + " " + new Date().toString());
                    }

                    this.h = System.currentTimeMillis();
                    int v1;
                    for (v1 = 0; v1 < 2; ++v1) {
                        long v4 = v2[v1];
                        if (v4 >= 0) {
                            if (this.e[v1] >= 0) {
                                v6 = v4 - this.e[v1];
                                if (v6 > 5242880) {
                                    v3 = new TrafficWarningInfo();
                                    v0_1 = v1 == 0 ? true : false;
                                    v3.tx = v0_1;
                                    v3.cost = v6;
                                    v3.last = this.e[v1];
                                    v3.current = v4;
                                    v3.lastTime = this.h;
                                    v3.initTime = this.g;
                                    v3.isAccumulate = false;
                                    if (this.c == null) {
                                    } else {
                                        this.c.onTrafficWarning(v3);
                                    }
                                }
                            }

                            this.e[v1] = v4;
                            if (this.f[v1] >= 0) {
                                v6 = v4 - this.f[v1];
                                if (v6 <= 20971520) {
                                    continue;
                                }

                                v3 = new TrafficWarningInfo();
                                v0_1 = v1 == 0 ? true : false;
                                v3.tx = v0_1;
                                v3.cost = v6;
                                v3.last = this.f[v1];
                                v3.current = v4;
                                v3.lastTime = this.h;
                                v3.initTime = this.g;
                                v3.isAccumulate = true;
                                if (this.c == null) {

                                } else {
                                    this.c.onTrafficWarning(v3);
                                }
                                continue;
                            }

                            this.f[v1] = v4;
                        }
                    }

                    if (this.i <= 0) {
                        this.i = 0;
                    }

                    if (this.j <= 0) {
                        this.j = 300000;
                    }

                    ++this.i;
                    if (this.i > 0 && this.i <= 5) {
                        this.j <<= 1;
                    }

                    this.a.sendEmptyMessageDelayed(1, this.j);
                    return;
                } catch (Throwable v0) {
                    return;
                }
            }
            case 2: {
                this.a.removeMessages(1);
                this.a.removeMessages(3);
                this.i = 0;
                this.j = 300000;
                this.a(this.f);
                this.e[0] = this.f[0];
                this.e[1] = this.f[1];
                this.g = System.currentTimeMillis();
                this.h = this.g;
                this.a.sendEmptyMessageDelayed(1, 300000);
                if (Logger.debug()) {
                    Logger.d("TrafficGuard", "init check traffic: " + this.i
                            + " " + this.f[0]
                            + " " + this.f[1]
                            + " " + new Date().toString());
                }
                return;
            }
            case 3: {
                this.a.removeMessages(1);
                this.a.removeMessages(2);
                this.i = 0;
                this.j = 0;
                return;
            }
        }
        return;


    }
}

